using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Dimok.Web.UI {

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Js. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class JS {
		private const string GRIDVIEWEX_JS = "Dimok.Web.UI.GridViewEx.js";

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Default constructor. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public JS() { }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Registers the scripts described by page. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="page">	The page. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public void RegisterScripts(Page page) {
			Type type = typeof(JS);
			string url = page.ClientScript.GetWebResourceUrl(type, GRIDVIEWEX_JS);
			if(!page.ClientScript.IsClientScriptIncludeRegistered(type, GRIDVIEWEX_JS))
				page.ClientScript.RegisterClientScriptInclude(type, GRIDVIEWEX_JS, url);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Registers the common includes described by page. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="page">	The page. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void RegisterCommonIncludes(Page page) {
			if (ScriptManager.GetCurrent(page).IsInAsyncPostBack) return;
			page.ClientScript.RegisterClientScriptInclude("common", "/include/common.js");
			page.ClientScript.RegisterClientScriptInclude("SOAP", "/include/SOAP.js");
			page.ClientScript.RegisterClientScriptInclude("Events", "/include/Events.js");
			page.ClientScript.RegisterClientScriptInclude("RDS", "/include/RDS.js");
			page.ClientScript.RegisterClientScriptInclude("XML", "/include/XML.js");
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Registers the j query described by page. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="page">	The page. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void RegisterJQuery(Page page) {
			RegisterResourceScript(page, "JQuery");
			RegisterResourceScript(page, "JQuery.watermark");
			RegisterResourceScript(page, "JQueryUI");
			RegisterResourceScript(page, "ui.sortable");

			RegisterResourceScript(page, "JQuery.dimensions");
			RegisterResourceScript(page, "JQuery.delegate");
			RegisterResourceScript(page, "JQuery.bgiframe");
			RegisterResourceScript(page, "JQuery.autocomplete");
			RegisterResourceScript(page, "JQuery.blockUI");
			RegisterResourceScript(page, "JQuery.tinysort");

			RegisterResourceCSS(page, "jquery.autocomplete");
			//RegisterResourceScript(page, "JQuery.validate");
			//RegisterResourceScript(page, "JQuery.autogrow");
			page.ClientScript.RegisterClientScriptResource(typeof(JS), "Dimok.Web.UI.JS.JQuery.jtemplates.js");
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Registers the resource script. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="page">	The page. </param>
		/// <param name="Name">	The name. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		static void RegisterResourceScript(Page page, string Name) {
			page.ClientScript.RegisterClientScriptInclude(typeof(JS), "Dimok.Web.UI.JS."+Name+".js",
				page.ClientScript.GetWebResourceUrl(typeof(JS), "Dimok.Web.UI.JS."+Name+".js"));
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Registers the resource css. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="Page">			The page. </param>
		/// <param name="cssName">	Name of the css. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private static void RegisterResourceCSS(Page Page,string cssName) {
			HtmlLink cssLink = new HtmlLink();
			cssLink.Href = Page.ClientScript.GetWebResourceUrl(typeof(JS), "Dimok.Web.UI.JS." + cssName + ".css");
			cssLink.Attributes.Add("type", "text/css");
			cssLink.Attributes.Add("rel", "stylesheet");
			Page.Header.Controls.Add(cssLink);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Registers the ajax toolkit calendar described by Page. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="Page">	The page. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void RegisterAjaxToolkitCalendar(Page Page) {
			// Register CSS styles
			HtmlLink cssLink = new HtmlLink();
			cssLink.Href = Page.ClientScript.GetWebResourceUrl(typeof(AjaxControlToolkit.CalendarExtender), "AjaxControlToolkit.Calendar.Calendar.css");
			cssLink.Attributes.Add("type", "text/css");
			cssLink.Attributes.Add("rel", "stylesheet");
			Page.Header.Controls.Add(cssLink);

			ScriptManager.RegisterClientScriptResource(Page, typeof(AjaxControlToolkit.PopupExtender), "AjaxControlToolkit.PopupExtender.PopupBehavior.js");
			ScriptManager.RegisterClientScriptResource(Page, typeof(AjaxControlToolkit.CommonToolkitScripts), "AjaxControlToolkit.Common.Threading.js");
			ScriptManager.RegisterClientScriptResource(Page, typeof(AjaxControlToolkit.DateTimeScripts), "AjaxControlToolkit.Common.DateTime.js");
			ScriptManager.RegisterClientScriptResource(Page, typeof(AjaxControlToolkit.CalendarExtender), "AjaxControlToolkit.Calendar.CalendarBehavior.js");
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Registers the admin ws described by page. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="page">	The page. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void RegisterAdminWS(Page page) { RegisterWS(page, "/Admin/Admin.asmx"); }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Registers the ws. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="page">						The page. </param>
		/// <param name="webServicePath">	Full pathname of the web service file. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void RegisterWS(Page page, string webServicePath) {
			RegisterCommonIncludes(page);
			if (ScriptManager.GetCurrent(page).IsInAsyncPostBack) return;
			string wsScript = @"
pageLoads.push(
function(){
	if( !Sys.WebForms || !Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack() )
		initWS('_WS_" +webServicePath+"','" + webServicePath + @"');
}
);
";
			page.ClientScript.RegisterStartupScript(page.GetType(), "InitWS", wsScript, true);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Registers the client script block. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="control">	The control. </param>
		/// <param name="script">		The script. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void RegisterClientScriptBlock(Control control, string script) {
			if (control.Page == null) return;
			if (ScriptManager.GetCurrent(control.Page).IsInAsyncPostBack)
				ScriptManager.RegisterClientScriptBlock(control.NamingContainer, control.NamingContainer.GetType(), control.ClientID, script, true);
			else
				control.Page.ClientScript.RegisterStartupScript(control.NamingContainer.GetType(), control.ClientID, script, true);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Script local i ds. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="parent">	The parent. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ScriptLocalIDs(object parent) {
			ScriptLocalIDs(parent, Find.ChildrenByType((Control)parent, typeof(Control)).ToArray());
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Script local i ds. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="parent">		The parent. </param>
		/// <param name="controls">	A variable-length parameters list containing controls. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ScriptLocalIDs(object parent,params Control[] controls) {
			foreach (Control control in controls)
				ScriptLocalID(parent, control);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Script local identifier. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="parent">		The parent. </param>
		/// <param name="control">	The control. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ScriptLocalID(object parent, Control control) {
			if ((control.ClientID + "") != "" && !Regex.IsMatch(control.ID + "", "^ctl\\d+$")) {
				string script = "function " + control.ID.Replace(" ", "$") + "(){return document.getElementById('" + control.ClientID + "')}\n";
				if (ScriptManager.GetCurrent(control.Page).IsInAsyncPostBack)
					ScriptManager.RegisterClientScriptBlock(control.NamingContainer, control.NamingContainer.GetType(), control.ClientID, script, true);
				else
					control.Page.ClientScript.RegisterStartupScript(control.NamingContainer.GetType(), control.ClientID, script, true);
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Script get. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="control">	The control. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ScriptGet(Control control) { ScriptLocal(control, "$get"); }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Script find. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="control">	The control. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void ScriptFind(Control control) { ScriptLocal(control, "$find"); }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Script local. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="control">				The control. </param>
		/// <param name="accessFunction">	The access function. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private static void ScriptLocal(Control control, string accessFunction) {
			if ((control.ClientID + "") != "" && !Regex.IsMatch(control.ID + "", "^ctl\\d+$")) {
				string script = "function " + control.ID + "(){return "+accessFunction+"('" + control.ClientID + "')}\n";
				if (ScriptManager.GetCurrent(control.Page).IsInAsyncPostBack)
					ScriptManager.RegisterClientScriptBlock(control.NamingContainer, control.NamingContainer.GetType(), control.ClientID, script, true);
				else
					control.Page.ClientScript.RegisterStartupScript(control.NamingContainer.GetType(), control.ClientID, script, true);
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Sets a profile i ds. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="controls">	A variable-length parameters list containing controls. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void SetProfileIDs(params HtmlControl[] controls) {
			foreach (var c in controls)
				c.SetProfileID();
		}
	}
}
